// pages/picDetails/picDetails.js
const app = getApp()
//const user = JSON.parse(wx.getStorageSync("user"));
const base_url = app.globalData.base_url;
const util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    empty:true,//无写真集
    load:true,//上传写真
    id:'',//狗牌id,
    delArr:[],//选中需要删除的写真的id数组
    page:{
      p:0,
      psize:100
    },
    del:false,
    imgPaths:[],//图片列表,
    dog_info:{},
    photo_list:[]//已上传图片列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id
    })
    this.init();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (ops) {
    if (ops.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: '我在法斗家的写真，快来看看吧！',
        path: '/pages/picDetails/picDetails?id=' + this.data.id,
        success: function (res) {
          // 转发成功
          var shareTickets = res.shareTickets;
          if (shareTickets.length == 0) {
            return false;
          }
          wx.getShareInfo({
            shareTicket: shareTickets[0],
            success: function (res) {
              var encryptedData = res.encryptedData;
              var iv = res.iv;
            }
          })
        },
        fail: function (res) {
          // 转发失败
          wx.showToast({
            icon: 'none',
            title: '转发失败',
          })
        }

      }
    }
  },
  upload:function(){

  },
  cancel(){
    this.setData({
      del:false
    })
  },
  init(){
    wx.showLoading({
      title: '加载中',
      success:()=>{
        wx.request({
          url: base_url + '/Photo/getList',
          data: {
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
            'dog_id': this.data.id,
            'p':this.data.page.p,
            'psize':this.data.page.psize
          },
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {
            if (res.data.success == '1' && res.data.data) {
              this.setData({
                dog_info:res.data.data.dog_info,
                photo_list: res.data.data.photo_list
              })
              if(this.data.photo_list != null){
                this.setData({
                  empty:false
                })
              }
            }
            wx.hideLoading();
          }
        })
      }
    })
  },
  upload(){
    wx.chooseImage({
      count: 9,
      success: res => {
        this.setData({
          imgPaths: res.tempFilePaths,
          empty:false
        })
        wx.showLoading({
          title: '正在上传图片',
          success: () => {
            let i = 0;
            let img = [];
            let callback = () => {
              wx.uploadFile({
                url: base_url + '/File/uploadImage',
                name: 'uploads',
                filePath: this.data.imgPaths[i],
                header: {
                  'content-type': 'multipart/form-data'
                },
                success: res => {
                  let data = JSON.parse(res.data);
                  if (data.success == '1') {
                    img.push(data.data.path)//拼成路径数组
                    i++;//上传张数
                    if (i == this.data.imgPaths.length) {//上传结束
                      let str = img.join(',');
                      wx.request({
                        url: base_url + '/Photo/add',
                        data: {
                          'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
                          'dog_id': this.data.id,
                          'photo_url': str
                        },
                        method: 'POST',
                        header: {
                          'content-type': 'application/x-www-form-urlencoded'
                        },
                        success: res => {
                          this.init();
                          if (res.data.success == '1') {
                            wx.showToast({
                              title: '上传成功',
                              mask: true,
                              icon: 'success',
                              duration: 2000
                            })
                          }
                          wx.hideLoading();
                        }
                      })
                      
                    } else {
                      callback()
                    }
                  }
                }
              })
            }
            callback()
          }
        })
      }
    })
  },
  selPic(e){//查看大图
    if(!this.data.del){
      wx.navigateTo({
        url: '../picShow/picShow?id=' + e.currentTarget.dataset.id
      })
    }else{
      let del = this.data.photo_list.map(v=>{
        if (v.photo_id == e.currentTarget.dataset.id){
          v.is_sel = (v.is_sel == '#00A0E9') ? '' : '#00A0E9'
        }
        return v
      })
      this.setData({
        photo_list:del
      })
    }
  },
  edit(){
    //打开删除写真
    this.setData({
      del:!this.data.del
    })
  },
  delete(){
    let arr = this.data.photo_list;
    let del = [];//删除下标数组
    for(let i = 0;i<arr.length;i++){
      if (arr[i].is_sel == '#00A0E9'){
        del.push(arr[i].photo_id)
      }
    }
    if(del == ''){
      wx.showToast({
        title: '您没有选择照片',
        icon:'none',
        duration:2000
      })
      return
    }
    wx.showModal({
      title: '提示',
      content: '汪，删除后不可恢复，确定删除吗？',
      success: (res)=> {
        if (res.confirm) {//确认删除
          wx.showLoading({
            success:()=>{
              wx.request({
                url: base_url + '/Photo/del',
                data: {
                  'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
                  'id': del.join(',')
                },
                method: 'POST',
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                success: res => {
                  this.setData({
                    del:false
                  })
                  wx.hideLoading();
                  if (res.data.success == '1') {
                    wx.showToast({
                      title: '删除成功',
                      icon: 'success',
                      duration: 2000
                    })
                    this.init()
                  }else{
                    wx.showToast({
                      title: '删除失败',
                      icon: 'none',
                      duration: 2000
                    })
                  }
                }
              })
            }
          })
        } else if (res.cancel) {
          wx.showToast({
            title: '已取消删除',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
    
  },
  isLike(e){//点击喜欢该写真
    let photo_list = this.data.photo_list;
    let id = e.currentTarget.dataset.id;
    photo_list.map(v=>{
      if(v.photo_id == id){
        v.is_like = v.is_like == 0 ? 1: 0;
        if(v.is_like == 1){
          v.like_num++
        }else{
          v.like_num--
        }
      }
      return v
    })
    this.setData({
      photo_list:photo_list
    })
    wx.request({
      url: base_url + '/Photo/likeOrCancel',
      data: {
        'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
        'id': id,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        if (res.data.success == '1') {
         
         
        }else{
          wx.showToast({
            title: '服务器出错啦',
            mask: true,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  }
})